package com.up.remoting.service.impl;

import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.up.remoting.Constants;
import com.up.remoting.service.SDK;
import com.up.remoting.util.TypeUtils;
import com.up.remoting.util.URLBuilder;

/**
 * @author xueqiang.mi
 * 
 */
public class SDKImpl implements SDK {

  private static AbstractRemotingService remotingService;
  private static SDK instance;

  public static SDK getInstance() {
    if (instance == null) {
      if (remotingService == null) {
        throw new NullPointerException("Please initialize this SDK before using it.");
      }

      instance = new SDKImpl();
    }

    return instance;
  }

  public static void init(AbstractRemotingService remotingService) {
    SDKImpl.remotingService = remotingService;
  }

  private SDKImpl() {
  }

  @Override
  public List<Map<String, String>> getBrands(int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_BRAND_LIST, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getCommentsByItem(int itemId, int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_COMMENT_BY_ITEM, itemId, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getCommentsByUser(int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_COMMENT_BY_USER, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public Map<String, String> getItemDetails(int itemId) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEM_DETAILS, itemId);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_MAP_STRING_STRING);
  }

  @Override
  public List<Map<String, String>> getItemDetailsAt(int offset, int size, int next) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEM_DETAILS_AT, offset, size, next);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemDetailsCommented(int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEM_DETAILS_COMMENTED, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemDetailsFocused(int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEM_DETAILS_FOCUSED, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemsByCategory(int cat_id, int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEMS_CATEGORY, cat_id, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemsByCollected(int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEMS_COLLECTED, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemsByFriends(int offset, int next) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEMS_FRIENDS, offset, next);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemsByHot(int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEMS_HOT, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemsByLatest(int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEMS_LATEST, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemsByLocation(int loc_id, int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEMS_LOCATION, loc_id, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getItemsByRelativeItem(int itemId) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEMS_RELATIVE_TO_ITEM, itemId);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getPricesByItem(int itemId) {
    String uri = URLBuilder.build(Constants.SDK_API_ITEM_PRICES, itemId);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public Map<String, String> getProfile(int userId) {
    String uri = URLBuilder.build(Constants.SDK_API_USER_BY_ID, userId);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_MAP_STRING_STRING);
  }

  @Override
  public Map<String, String> getProfile(String email) {
    String uri = URLBuilder.build(Constants.SDK_API_USER_BY_EMAIL, email);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_MAP_STRING_STRING);
  }

  @Override
  public List<Map<String, String>> getShopEvents() {
    String uri = URLBuilder.build(Constants.SDK_API_SHOP_EVENTS);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getShops() {
    String uri = URLBuilder.build(Constants.SDK_API_SHOPS);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<String> getShopTips() {
    String uri = URLBuilder.build(Constants.SDK_API_SHOP_TIPS);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_STRING);
  }

  @Override
  public List<Integer> getTagsByItem(int itemId) {
    String uri = URLBuilder.build(Constants.SDK_API_TAG_BY_ITEM, itemId);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_INTEGER);
  }

  @Override
  public List<Integer> getTagsByUser(int userId) {
    String uri = URLBuilder.build(Constants.SDK_API_TAG_BY_USER, userId);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_INTEGER);
  }

  @Override
  public List<Map<String, String>> getUserBrands(int userId) {
    String uri = URLBuilder.build(Constants.SDK_API_USER_BRANDS, userId);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getUserFriends(int userId, int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_USER_FRIENDS, userId, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getUserItemsCollected(int userId, int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_USER_ITEMS_COLLECTED, userId, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getUserItemsLiked(int userId, int offset) {
    String uri = URLBuilder.build(Constants.SDK_API_USER_ITEMS_LIKED, userId, offset);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

  @Override
  public List<Map<String, String>> getUserItemsPublished(int userId, int offset, int size) {
    String uri = URLBuilder.build(Constants.SDK_API_USER_ITEMS_PUBLISHED, userId, offset, size);
    String data = remotingService.get(uri);

    return new Gson().fromJson(data, TypeUtils.TYPE_LIST_MAP);
  }

}
